package server

import (
	"rongyy/src/middlewares"
	"rongyy/src/models"
	"rongyy/src/service"
	"time"
)

func PaymentOrdersRecording() {
	var timer *time.Timer
	go func() {
		for {
			d := time.Duration(-60) * time.Second
			ct := time.Now().Local().Add(d).Format("15:04:03")
			date := time.Now().Format("20060102")
			hour := ct[0:2]
			min := ct[3:5]
			ct1 := time.Now().Local().Format("15:04:03")
			minEnd := ct1[3:5]
			req := &models.ReqPaymentQueryByTime{
				Start:      date,
				StartHour:  hour,
				StartMin:   min,
				End:        date,
				EndHour:    hour,
				EndMin:     minEnd,
				Page:       "1",
				Kind:       "0",
				BuffNumber: 120000,
			}
			_, err := (&service.PaymentService{}).QueryByTime(req)
			//history := endoscopic.SetMsgHistory{}
			//data, err := history.SelectAll()
			if err != nil {
				//middlewares.Logger.Println("获取银行订单记录错误")
			}
			//middlewares.Logger.Println("获取银行订单记录成功")
			//for _, value := range data {
			//	req := &req2.SetMsgReq{}
			//	req.TemplateId = value.TemplateId
			//	req.PatientIds = make([]string, 1)
			//	req.PatientIds[0] = value.PatientId
			//	d, e := (&endoscopic2.EndoscopicService{}).SendMsg(req)
			//	if e == nil {
			//		value.IsSend = 1
			//		history.Update(&value)
			//		fmt.Println("send msg:", d)
			//	}
			//
			//}
			//fmt.Println("send msg end")
			timer = time.NewTimer(25 * time.Second)
			<-timer.C
		}
	}()
}

func OrderScanner() {
	var timer *time.Timer
	go func() {
		for {
			// 挂号部分
			regs, total, err := (&models.RegisterModel{}).QueryUnconfirmOrder()
			if err != nil {
				//middlewares.Logger.Println(err)
				return
			}
			if total > 0 {
				for _, item := range regs {
					order, has, err := (&models.PaymentOrders{}).FindByOrder(item.OrderSn)
					if err != nil {
						//middlewares.Logger.Println(err)
						return
					}
					if has && order.OrderStatus == 1 {
						// 订单支付成功，直接去确认
						rm := &models.RegisterModel{}
						reg, has, err := rm.FindByOrderSn(item.OrderSn)
						//middlewares.Logger.Println("OrderScanner 查询支付订单信息: - ", reg)
						if has && err == nil {
							// 普通挂号部分
							if reg.Status == "confirm" && reg.PayStatus == "paid" {
								return
							} else if reg.Status == "locked" && reg.PayStatus == "paying" && reg.AppOrderCode == "" {
								reqReg := &models.Req1101Params{
									TransactionId:    reg.TransactionId,
									ScheduleItemCode: reg.ScheduleItemCode,
									PatientID:        reg.PatientId,
									PayModeCode:      "JHZF",
									PayFee:           reg.Fee,
									QueueNo:          "",
									PayAmt:           reg.Fee,
									PlatformNo:       item.OrderSn,
									OutPayNo:         "",
									PayChannel:       "",
									POSPayStr:        "",
									PayDate:          order.TranDate[0:10],
									PayTime:          order.TranDate[11:],
									UpdateBy:         "scanner",
								}
								_, err = (&service.RegistrationService{}).Register(reqReg)
							}

							if reg.Status == "locked" && reg.PayStatus == "paying" && reg.AppOrderCode != "" {
								reqRegApp := &models.Req1101Params{
									TransactionId: reg.TransactionId,
									PatientID:     reg.PatientId,
									PayModeCode:   "JHZF",
									PayFee:        reg.Fee,
									QueueNo:       "",
									PayAmt:        reg.Fee,
									PlatformNo:    item.OrderSn,
									OutPayNo:      "",
									PayChannel:    "",
									POSPayStr:     "",
									PayDate:       order.TranDate[0:10],
									PayTime:       order.TranDate[11:],
									UpdateBy:      "scanner",
									AppOrderCode:  reg.AppOrderCode,
								}
								_, err = (&service.RegistrationService{}).RegisterZJ(reqRegApp)
							}
						}
					}
				}
			}
			// 门诊部分
			ops, total, err := (&models.PrescriptionModel{}).QueryUnconfirmOrder()
			if err != nil {
				//middlewares.Logger.Println(err)
				return
			}
			if total > 0 {
				for _, item := range ops {
					order, has, err := (&models.PaymentOrders{}).FindByOrder(item.OrderSn)
					if err != nil {
						//middlewares.Logger.Println(err)
						return
					}
					if has && order.OrderStatus == 1 {
						// 订单支付成功，直接去确认
						pm := &models.PrescriptionModel{}
						ppm, has, err := pm.FindByOrderSn(item.OrderSn)
						middlewares.Logger.Println("主动查询支付订单信息: - ", ppm)
						if has && err == nil {
							if ppm.Status == "confirm" && ppm.PayStatus == "paid" {
								return
							} else if ppm.Status == "prepay" && ppm.PayStatus == "paying" {
								reqOp := &models.Req4906Params{
									PatientId:    ppm.PatientId,
									CardNo:       "",
									OrderNo:      ppm.OrderNo,
									InvoiceNoStr: ppm.InvoiceNo,
									PayAmt:       order.PaymentMoney,
									PlatformNo:   ppm.OrderSn,
									OutPayNo:     ppm.OrderNo,
									PayDate:      order.TranDate[0:10],
									PayTime:      order.TranDate[11:],
									UpdatedBy:    "scanner",
									OrderSn:      item.OrderSn,
								}
								_, err = (&service.OutPatientService{}).CompleteCharge(reqOp)
							}
						}
					}
				}
			}
			timer = time.NewTimer(23 * time.Second)
			<-timer.C
		}
	}()
}
